<?php


namespace backend\modules\statistics\controllers;

use backend\components\Helper;
use backend\controllers\AdminController;
use backend\modules\statistics\models\UserData;
use common\models\business\BusinessUser;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
use Yii;
/**
 * Class SiteController
 * @package backend\modules\finance\controllers
 * 用户统计相关
 */
class UserController extends AdminController
{





    public function actionIndex()
    {
        $starttime = '2017-05-01';
        $query = (new \yii\db\Query())
            ->select('count("id") as num,FROM_UNIXTIME(createtime) as time')
            ->from(BusinessUser::tableName())
            ->where('createtime>=:start and createtime<=:now',[':start'=>strtotime($starttime),':now'=>time()])
            ->groupBy('FROM_UNIXTIME(createtime)')
            ->indexBy('time');

        $users = [];
        foreach ($query->batch() as $user) {
            $tmpusers = ArrayHelper::getColumn($user,'time');
            $dissdays = \common\components\Helper::dateDifference($starttime,date('Y-m-d 00:00:00'));
            for ($i=0; $i<=$dissdays; $i++){
                $benddata = date('Y-m-d 00:00:00',strtotime($starttime.' +'.$i.' day'));
                if(in_array($benddata,$tmpusers)){
                   $users[] = intval($user[$benddata]['num']);
                }else{
                   $users[] = 0;
                }
            }
        }


        $genderdata = UserData::gender();

        return $this->render('index',[
            'user' => \GuzzleHttp\json_encode($users),
            'genderdata' => $genderdata
        ]);
    }


    //增长率
//    public function actionIndex()
//    {
//        $starttime = '2017-05-01';
//        $query = (new \yii\db\Query())
//            ->select('count("id") as num,FROM_UNIXTIME(createtime) as time')
//            ->from(BusinessUser::tableName())
//            ->where('createtime>=:start and createtime<=:now',[':start'=>strtotime($starttime),':now'=>time()])
//            ->groupBy('FROM_UNIXTIME(createtime)')
//            ->indexBy('time');
//
//        $users = [];
//        foreach ($query->batch() as $user) {
//            $tmpusers = ArrayHelper::getColumn($user,'time');
//            $dissdays = \common\components\Helper::dateDifference($starttime,date('Y-m-d 00:00:00'));
//            for ($i=0; $i<=$dissdays; $i++){
//                $benddata = date('Y-m-d 00:00:00',strtotime($starttime.' +'.$i.' day'));
//                $predata = date('Y-m-d 00:00:00',strtotime($starttime .' +'.($i-1).' day'));
//                if(in_array($benddata,$tmpusers)){
//                    if(in_array($predata,$tmpusers)){
//                        $num = intval($user[$predata]['num']);
//                    }else{
//                        $num = 0;
//                    }
//                    $users[] = (intval($user[$benddata]['num'])-$num)/intval($user[$benddata]['num']);
//                }else{
//                    $users[] = 0;
//                }
//            }
//        }
//
//        return $this->render('index',[
//            'user' => \GuzzleHttp\json_encode($users)
//        ]);
//    }




}




